package com.battery.web.controller.system.battery;

import java.util.List;

import com.battery.system.service.battery.INoticeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.Notice;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * APP公告Controller
 *
 * @author battery
 * @date 2021-08-17
 */
@Controller
@RequestMapping("/system/battery/notice")
public class NoticeController extends BaseController {
    private String prefix = "system/battery/notice";

    @Autowired
    private INoticeService noticeService;

    @RequiresPermissions("system:battery:notice:view")
    @GetMapping()
    public String notice() {
        return prefix + "/notice";
    }

    /**
     * 查询APP公告列表
     */
    @RequiresPermissions("system:battery:notice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Notice notice) {
        startPage();
        List<Notice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 导出APP公告列表
     */
    @RequiresPermissions("system:battery:notice:export")
    @Log(title = "APP公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Notice notice) {
        List<Notice> list = noticeService.selectNoticeList(notice);
        ExcelUtil<Notice> util = new ExcelUtil<Notice>(Notice.class);
        return util.exportExcel(list, "notice");
    }

    /**
     * 新增APP公告
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存APP公告
     */
    @RequiresPermissions("system:battery:notice:add")
    @Log(title = "APP公告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Notice notice) {
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改APP公告
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Notice notice = noticeService.selectNoticeById(id);
        mmap.put("notice", notice);
        return prefix + "/edit";
    }

    /**
     * 修改保存APP公告
     */
    @RequiresPermissions("system:battery:notice:edit")
    @Log(title = "APP公告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Notice notice) {
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除APP公告
     */
    @RequiresPermissions("system:battery:notice:remove")
    @Log(title = "APP公告", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        if (StringUtils.isBlank(ids)) {
            return error("ids为空");
        }
        if (ids.contains(",")) {
            return error("暂时不支持批量操作");
        }
        return toAjax(noticeService.deleteLogicNoticeByIds(ids));
    }
}
